﻿namespace TSharp.Core.Util
{
    using TSharp.Core.Util;

    /// <summary>
    /// 分页查询
    /// </summary>
    public class QueryPage
    {
        /// <summary>
        /// 查询标识
        /// </summary>
        private readonly string qfix = "";

        private int _Index = -1;
        private int _Size = 20;
        private ParameterizedUri curl;

        /// <summary>
        /// 分页查询
        /// </summary>
        public QueryPage()
        {
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="fix"></param>
        public QueryPage(string fix)
        {
            qfix = fix;
        }

        /// <summary>
        /// 分页查询构造
        /// </summary>
        /// <param name="url"></param>
        public QueryPage(ParameterizedUri url)
        {
            Init(url);
            curl = url;
        }

        /// <summary>
        /// Gets or sets the index.
        /// </summary>
        /// <value>The index.</value>
        public int Index
        {
            get { return _Index; }
            set { _Index = value; }
        }

        /// <summary>
        /// Gets or sets the size.
        /// </summary>
        /// <value>The size.</value>
        public int Size
        {
            get { return _Size; }
            set { _Size = value; }
        }

        /// <summary>
        /// Gets or sets the total.
        /// </summary>
        /// <value>The total.</value>
        public int Total { get; set; }

        /// <summary>
        /// 分页查询初始
        /// </summary>
        /// <param name="url"></param>
        public void Init(ParameterizedUri url)
        {
            _Index = url.Queries[qfix + "page"].ToInt32(-1);
            // this._Size = SafeConvert.ToInt32(url.Queries[qfix + "-detectorObj"], 20);
            curl = url;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ParameterizedUri SetQuery(ParameterizedUri url)
        {
            url.SetQuery(qfix + "page", _Index);
            //url.SetQuery(qfix + "-detectorObj", this._Size);
            curl = url;
            return url;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public QueryPage SetPageSize(int size)
        {
            Size = size;
            return this;
        }

        /// <summary>
        /// 返回Url的表现形式
        /// <para>by tangjingbo at 2010/1/10 0:53</para>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (curl != null)
            {
                return curl;
            }
            return null;
        }
    }
}